<template>
  <div class="login">
    <div class="box">
      <img src="@/assets/common/login-logo.png" alt="" class="logo">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button class="btn" @click="btnClick">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { login } from '@/api/user'
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      isClick: true,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确手机号'))
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '请输入正确密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['getToken']),
    btnClick() {
      if (this.isClick) {
        this.$refs.form.validate(async(result) => {
          if (result) {
            await this.$store.dispatch('user/getToken', this.form)
            // 登录成功后进行路由跳转  回调处理
            const redirect = this.$route.query.redirect
            // console.log(redirect)
            this.$router.push(redirect === undefined ? '/' : redirect)
            this.$message.success('登录成功')
            this.isClick = false
            setTimeout(() => {
              this.isClick = true
            }, 5000)
          }
        })
      } else {
        this.$message.info('不要频繁登录')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    .logo {
      text-align: center;
      padding-bottom: 20px;
    }
    .btn {
      width: 100%;
      padding: 15px;
      background-color: #2162fa;
      border: none;
    }
  }
}
</style>
